﻿Public Class frmJournalReglement
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing
    Private m_SortingColumn As ColumnHeader

    Private Sub frmJournalReglement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TE_DATE.Value = Now
        BO_REMISE.Text = "Agence " & gUserAgence
        InitListeReglements()
    End Sub

    Private Sub InitListeReglements()

        Dim rs2 As ADODB.Recordset = Nothing
        Dim nb As Integer = 0
        Dim total As Decimal = 0

        ' effacer la listview
        LV_JOURNAL.Items.Clear()

        'RAZ total
        total = 0

        ' Lire les réglements
        szSQL = "CALL spGet_ELEVES_CPT_byDATE(" & gUserAgence & ",'" & Format(TE_DATE.Value, "yyyy/MM/dd") & "','CRE')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_JOURNAL.Items.Add(rs.Fields("cd_cli").Value)
            'lire la fiche Elève
            szSQL = "CALL spGet_ELEVES_byCD_CLI(" & rs.Fields("cd_cli").Value & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs2)
            If rs2.RecordCount > 0 Then
                LV_JOURNAL.Items(nb).SubItems.Add(nv(rs2.Fields("nompren").Value))
                LV_JOURNAL.Items(nb).SubItems.Add(nv(rs2.Fields("permis_demande").Value))
            Else
                LV_JOURNAL.Items(nb).SubItems.Add("")
                LV_JOURNAL.Items(nb).SubItems.Add("")
            End If
            LV_JOURNAL.Items(nb).SubItems.Add(nv(rs.Fields("intitule").Value))
            LV_JOURNAL.Items(nb).SubItems.Add(rs.Fields("credit").Value)
            ' libelle du mode de règlement
            LV_JOURNAL.Items(nb).SubItems.Add(getLibelleByCode("p_mode_regl", nv(rs.Fields("mode_regl").Value)))
            LV_JOURNAL.Items(nb).SubItems.Add(nv(rs.Fields("cd_uti").Value))
            total = total + rs.Fields("credit").Value
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        TE_TOTAL.Text = total & " €"
        If nb > 0 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
            BO_REMISE.Enabled = True
            BO_REMISE_ALL.Enabled = True
        Else
            LB_NbLignes.Text = "0 ligne"
            BO_REMISE.Enabled = False
            BO_REMISE_ALL.Enabled = False
        End If

    End Sub

    Private Sub TE_DATE_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TE_DATE.ValueChanged
        InitListeReglements()
    End Sub

    Private Sub BO_REMISE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_REMISE.Click
        ' édition journal des régelements de l'agence en cours
        gDate_debut = TE_DATE.Value
        FrmViewReport.pCodeEtat = 15
        FrmViewReport.pParamRPT = ""
        FrmViewReport.ShowDialog()
    End Sub

    Private Sub BO_REMISE_ALL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_REMISE_ALL.Click
        ' édition journal des régelements de tous les agences
        gDate_debut = TE_DATE.Value
        FrmViewReport.pCodeEtat = 45
        FrmViewReport.pParamRPT = ""
        FrmViewReport.ShowDialog()
    End Sub

    Private Sub LV_JOURNAL_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_JOURNAL.ColumnClick
        SortListView(LV_JOURNAL, m_SortingColumn, e.Column)
    End Sub

End Class